<?php

namespace backend\controllers;

use backend\models\EveryIsOk;
use backend\models\PermissionForm;
use Yii;
use backend\models\Menu;
use yii\data\ActiveDataProvider;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\web\HttpException;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * MenuController implements the CRUD actions for Menu model.
 */
class MenuController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
            'access'=>[
                'class'=>AccessControl::className(),
                'rules'=>[
                    [
                        'allow'=>true,
                        'actions'=>[
                            'index','view','create',
                            'update','delete',''
                        ],
                        'matchCallback'=>function ($rules,$action){
                            //判断当前用户是否拥有该操作的访问权限
                            if(!\Yii::$app->user->can($this->route)){

                                //判断当前用户是否是游客,如果是,跳转到登录页面
                                if(\Yii::$app->user->isGuest){
                                    return $action->controller->redirect(\Yii::$app->user->loginUrl);
                                }
                                //没有权限,抛出一个403 forbidden 异常

                                throw new HttpException(403,'对不起,你没有该操作的权限');

                                //return false;
                            }
                            return true;
                        }
                    ],
                ]
            ]
        ];
    }

    /**
     * Lists all Menu models.
     * @return mixed
     */
    public function actionIndex()
    {
        $dataProvider = new ActiveDataProvider([
            'query' => Menu::find(),
        ]);

        return $this->render('index', [
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single Menu model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new Menu model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new Menu();
        //设置默认选中
        $model->parent_id = 1;

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            //  return $this->redirect(['view', 'id' => $model->id]);
            //添加菜单成功后,自动添加一条对应的权限
            //菜单名称作为权限的描述,菜单的路由作为权限的名称
            //实例化RBAC权限认证管理对象
            //调用权限表单模型上的静态方法添加权限
            $rs = PermissionForm::savePermission($model->route,$model->name);
            //调用方法设置提示信息
            EveryIsOk::setTisiInfo($rs,'添加菜单成功,相应的权限添加');
           //跳转到添加相应的权限的页面
            //return $this->redirect(['rbac/add-permission']);
            return $this->redirect(['menu/index']);
        } else {
            $models = Menu::getParentInfo();
            return $this->render('create', [
                'model' => $model,
                'models'=>$models,
            ]);
        }
    }

    /**
     * Updates an existing Menu model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */

    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {

            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            $models = Menu::getParentInfo();
            return $this->render('update', [
                'model' => $model,
                'models'=>$models,
            ]);
        }
    }

    /**
     * Deletes an existing Menu model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */

    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the Menu model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Menu the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Menu::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
